package UtilizatoriHardware;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// printStackTrace();
@SuppressWarnings("CallToThreadDumpStack")
//

public class Utilizatori_Hardware {
     public Utilizatori_Hardware() {
        dbUrl = "jdbc:postgresql://192.168.61.205/license?user=postgres&password=telinit";
        String driverName = "org.postgresql.Driver";
        loadDatabaseDriver(driverName);
    }


    private void loadDatabaseDriver(String driverName) {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public boolean connect() {
        try {
            // jdbc.postgresql.org/documentation/84/index.html
            dbConnection = DriverManager.getConnection(dbUrl);
            System.out.println ("Database connection established");
            isConnected = dbConnection != null;
        } catch (SQLException ex) {
            System.out.println ("Database connection couldn't be established");
            isConnected = false;
        }
        return isConnected;
    }


    public void disconnect() {
        if(isConnected) {
            try {
                DriverManager.getConnection(dbUrl).close() ;
                System.out.println ("Database connection terminated");
            } catch (SQLException ex) {
                //
            }
            isConnected = false;
        }
    }


    public List<Utilizatori_Licente> getListEntries() {
        List<Utilizatori_Licente> userList = new ArrayList<Utilizatori_Licente>();
        Statement queryStatement = null;
        ResultSet results = null;
        try {
            queryStatement = dbConnection.createStatement();
            results = queryStatement.executeQuery(strGetListEntries);
            while(results.next()) {
                Long user_id = results.getLong(1);
                Long hardware_id = results.getLong(2);
                String user_ip = results.getString(3);
                String user_first_name = results.getString(4);
                String user_last_name = results.getString(5);
                String name = results.getString(6);
                String manufacturer_brand = results.getString(7);
                String type = results.getString(8);
                //String manufacturer_brand = result.getString(9);
                Utilizatori_Licente entry = new Utilizatori_Licente(user_id, hardware_id, user_ip, user_first_name, user_last_name, name, type, manufacturer_brand);
                userList.add(entry);
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
        return userList;
    }

    public static void main(String[] args) {
        Utilizatori_Hardware db = new Utilizatori_Hardware();
        db.connect();
        List<Utilizatori_Licente> entries = db.getListEntries();
        Iterator i = entries.iterator();
        while(i.hasNext())
        {
          Utilizatori_Licente value= (Utilizatori_Licente) i.next();
          System.out.println(value.user_id() + " - " + value.hardware_id() + " - " +
                  value.user_ip()+ " - " + value.user_first_name()+ " - " + value.user_last_name()+ " - " + value.name()+ " - " + value.type()+ " - "+ value.manufacturer_brand());
        }
        db.disconnect();
    }

    private Connection dbConnection;
    private boolean isConnected;
    private String dbUrl;

    private static final String strGetListEntries =
            " SELECT * from Utilizatori_Hardware  ";


}
